<?php
class LoginController {
	function doControll() {
		$loginHandler = new LoginHandler();
		$loginView = new LoginView();
		$container = $loginView -> createContainer();
		$content = $container;
		
		$logoutBox = $loginView -> createLoggedInBox();
		$loginBox = $loginView -> createLoginBox();
	
		if ($loginHandler -> isLoggedIn()) {
			// If user is logged in, add 'log out'-box to body-variable
			$content = $logoutBox;

			// If user tried to logout
			if ($loginView -> triedToLogout()) {
				// Create 'logged out' feedback message and add to body-variable.
				// Log out
				$loginHandler -> doLogout();
				$content = $loginBox;
			}
		} else {
			// If user is logged out, add 'log in'-box to body-variable
			$loginBox = $loginView -> createLoginBox();
			$content = $loginBox;

			// If user tried to Login
			if ($loginView -> triedToLogin()) {
				// Get input name and password
				$name = $loginView -> getUserName();
				$password = $loginView -> getPassword();
				// Login
				if ($loginHandler -> doLogin($name, $password)) {
					// If login succeeded
					$content = $logoutBox;
				} else {
					// If login failed
					$feedback = $loginView->showFeedback($loginView -> errormessage);
					$content = $feedback . $loginBox;
				}
			}
		}
		return $container . $content;
	}
}
?>